package com.heima.search.service.impl;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.search.UserSearchDto;
import com.heima.model.common.user.entity.ApUser;
import com.heima.search.entity.ApAssociateWords;
import com.heima.search.service.ApAssociateWordsService;
import com.heima.search.utils.AppUserThreadLocalUtil;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ApAssociateWordsServiceImpl implements ApAssociateWordsService {
   @Resource
   private MongoTemplate mongoTemplate;
    @Override
    public ResponseResult search(UserSearchDto dto) {

        if (dto==null || dto.getSearchWords()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        if (dto.getPageSize()>=20) {
            dto.setPageSize(20);
        }

        Query query = Query.query(Criteria.where("associateWords").regex(".*?"+dto.getSearchWords()+".*")).limit(dto.getPageSize());
        List<ApAssociateWords> apAssociateWords = mongoTemplate.find(query, ApAssociateWords.class);

        return ResponseResult.okResult(apAssociateWords);
    }
}
